#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./esb/esb/routers.py
# Compiled at: 2017-11-16 15:44:28
"""
Tencent is pleased to support the open source community by making 蓝鲸智云(BlueKing) available.
Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
"""
import os, functools
from django.conf import settings
from django.http import Http404
from common.errors import error_codes
from common.constants import API_TYPE_OP
from esb.channel import get_channel_manager
from esb.component import get_components_manager
from esb.component.buffet import get_buffet_comp_manager
try:
    os.chdir(settings.BASE_DIR)
except:
    pass

def router_view(channel_type, request, path):
    components_manager = get_components_manager()
    channel_manager = get_channel_manager()
    path = '/%s/' % path.strip('/')
    request.g.comp_path = path
    channel_conf = channel_manager.get_channel_by_path(path, request.method)
    if not channel_conf:
        channel_conf, path_vars = channel_manager.search_channel_by_repath(path, request.method)
        request.g.path_vars = path_vars
    if not channel_conf:
        raise Http404
    esb_channel = channel_conf['channel']
    if not esb_channel.is_active:
        raise error_codes.INACTIVE_CHANNEL
    comp_cls = components_manager.get_comp_by_name(esb_channel.component_codename)
    if not comp_cls:
        raise error_codes.COMPONENT_NOT_FOUND.format_prompt(esb_channel.component_codename)
    channel_class = channel_conf['classes'][channel_type]
    channel_obj = channel_class(comp_cls, path=path, is_active=True, comp_conf=channel_conf.get('comp_conf'), channel_conf=channel_conf.get('channel_conf', {}))
    if getattr(esb_channel, 'request_validators', None) is not None:
        channel_obj.set_request_validators(esb_channel.request_validators)
    if getattr(esb_channel, 'append_request_validators', None) is not None:
        channel_obj.append_request_validators(esb_channel.append_request_validators)
    try:
        timeout_time = timeout_handler(esb_channel, comp_cls)
    except:
        timeout_time = settings.REQUEST_TIMEOUT_SECS

    request.g.timeout = timeout_time
    request.g.sys_name = comp_cls.sys_name
    return channel_obj.handle_request(request)


def timeout_handler(esb_channel, comp_cls):
    timeout_time = esb_channel.timeout_time
    execute_timeout = esb_channel.component_system.execute_timeout
    query_timeout = esb_channel.component_system.query_timeout
    if timeout_time or comp_cls.api_type != 'unknown':
        if comp_cls.api_type == API_TYPE_OP:
            timeout_time = execute_timeout if 1 else query_timeout
        else:
            timeout_time = execute_timeout if esb_channel.type == 1 else query_timeout
    return timeout_time


api_router_view = functools.partial(router_view, 'api')

def buffet_component_view(request, path):
    """
    处理自助接入组件的View
    """
    from esb.component.buffet import make_buffet_component_class
    channel_manager = get_channel_manager()
    buffet_comp_manager = get_buffet_comp_manager()
    path = '/%s/' % path.strip('/')
    request.g.comp_path = path
    buffet_comp_conf, path_vars = buffet_comp_manager.search_buffet_component(path, request.method)
    if not buffet_comp_conf:
        raise Http404
    buffet_comp_obj = buffet_comp_conf['obj']
    request.g.path_vars = path_vars
    comp_cls = make_buffet_component_class(buffet_comp_obj)
    if not comp_cls:
        raise error_codes.COMPONENT_NOT_FOUND
    channel_class = channel_manager.get_default_channel_classes()['api']
    channel_obj = channel_class(comp_cls, path=path, is_active=True)
    try:
        timeout_time = timeout_handler_for_buffet(buffet_comp_obj)
    except:
        timeout_time = settings.REQUEST_TIMEOUT_SECS

    request.g.timeout = timeout_time
    return channel_obj.handle_request(request)


def timeout_handler_for_buffet(comp_obj):
    """针对自助接入接口的超时时间"""
    timeout_time = comp_obj.timeout_time
    if timeout_time or comp_obj.type == 2:
        timeout_time = comp_obj.system.query_timeout if 1 else comp_obj.system.execute_timeout
    return timeout_time
